package com.minibobbo.utilities;

import com.badlogic.gdx.ApplicationListener;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.Input.Buttons;
import com.badlogic.gdx.Input.Keys;
import com.badlogic.gdx.graphics.Color;
import com.badlogic.gdx.graphics.GL10;
import com.badlogic.gdx.graphics.OrthographicCamera;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.g2d.BitmapFont;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.math.MathUtils;
import com.badlogic.gdx.math.Vector2;
import com.badlogic.gdx.math.Vector3;
import com.minibobbo.utilities.animobject.AnimObject;
import com.minibobbo.utilities.animobject.AnimObject.AnchorPoint;
import com.minibobbo.utilities.camerautilities.CameraUtil;
import com.minibobbo.utilities.particles.ParticleSystem;
import com.minibobbo.utilities.spe.SimplePlatformEngine;
import com.minibobbo.utilities.spe.SpeDynamicObject;
import com.minibobbo.utilities.spe.SpeMap;
import com.minibobbo.utilities.spe.actionmodules.SpeActionJetpack;
import com.minibobbo.utilities.spe.actionmodules.SpeActionSword;
import com.minibobbo.utilities.spe.aimodules.SpeAIWalkBackAndForth;
import com.minibobbo.utilities.spe.controlmodule.SpeControlWASD;
import com.minibobbo.utilities.spe.interactions.SpeIntDestroyOnAttack;
import com.minibobbo.utilities.spe.movementmodules.SpeMoveRunJumpWallJump;
import com.minibobbo.utilities.spe.movementmodules.SpeMoveWalkRunJump;
import com.minibobbo.utilities.spe.physicsmodules.SpePhysicsDestroyOnTouch;
import com.minibobbo.utilities.spe.physicsmodules.SpePhysicsSolidGravity;

public class Utility implements ApplicationListener {
	private OrthographicCamera camera;
	private SpriteBatch batch;

	private boolean paused = false;

	ParticleSystem ps;

	SpeDynamicObject player;
	SimplePlatformEngine spe;
	SpeMap map;

	CameraUtil cu;

	BitmapFont bf;

	AnimObject playerAnim;

	@Override
	public void create() {
		float w = Gdx.graphics.getWidth();
		float h = Gdx.graphics.getHeight();

		ps = new ParticleSystem();
		ps.setGravity(0, -6);
		camera = new OrthographicCamera(20, 20 * (h / w));
		batch = new SpriteBatch();

		playerAnim = new AnimObject();
		Texture t = new Texture(Gdx.files.internal("data/spy.png"));
		playerAnim.splitTexture(t, 33, 32);

		playerAnim.addLabel("run", 1, 6, .4f);
		playerAnim.setLabel("run");

		bf = new BitmapFont();

		cu = new CameraUtil(camera);

		// cu.transitionTo(2, 20, .5f);

		spe = new SimplePlatformEngine(camera);
		spe.setGravity(0, 25);
		map = spe.createMap("level/0/-2");
		map.setCollisionLayer(1, false);

		for (int i = 0; i < 0; i++) {
			SpeDynamicObject o = spe.addDynamicObject(new SpeDynamicObject());
			o.addMovementModule(new SpeMoveWalkRunJump());
			o.addPhysicsModule(new SpePhysicsSolidGravity());
			o.addAIModule(new SpeAIWalkBackAndForth());
			o.addInteraction(new SpeIntDestroyOnAttack());
			o.setBounds(MathUtils.random(3f, map.map.width),
					MathUtils.random(3f, map.map.height), 1, 1);

		}
		
		
		player = spe.addDynamicObject(new SpeDynamicObject());
		player.addPhysicsModule(new SpePhysicsSolidGravity());
		player.addMovementModule(new SpeMoveRunJumpWallJump().setJumpButton(1).setRunButton(0).setJumpStrength(15).setWallJumpStrength(8, 12));

		// player.addMovementModule(new SpeMovementGliding());
		player.addControlModule(new SpeControlWASD()).assignButton(3, Keys.Q)
				.assignButton(4, Keys.E);
		player.addActionModule(new SpeActionSword()).setButton(3)
				.setCooldown(1f);
		player.addActionModule(new SpeActionJetpack().setMaxVerticalSpeed(8)
				.setJetPackStrength(50).setButton(4));

		player.setBounds(7, 2, .5f, .9f);
		player.setPrimaryData("player");

		playerAnim.setRectangle(player.loc);
		playerAnim.setDrawSize(1.5f, 1.3f);
		playerAnim.setAnchorPoint(AnchorPoint.BOTTOM_CENTER);
		
		spe.debugVoxels = false;

		// player.applyForce(-1, 5);
		// player.stunMe(2.5f);

	}

	@Override
	public void dispose() {
		batch.dispose();
	}

	@Override
	public void render() {
		Gdx.gl.glClearColor(.5f, .5f, .5f, 1);
		Gdx.gl.glClear(GL10.GL_COLOR_BUFFER_BIT);
		Vector3 temp = new Vector3(Gdx.input.getX(), Gdx.input.getY(), 0);
		camera.unproject(temp);

		camera.position.x = player.loc.x;
		camera.position.y = player.loc.y;
		camera.update();

		if (Gdx.input.justTouched() && Gdx.input.isButtonPressed(Buttons.LEFT)) {
			// cu.shakeCamera(2, 8);
			// cu.transitionTo(temp.x, temp.y, .5f);
			float a = new Vector2(temp.x - player.loc.getCenter().x, temp.y
					- player.loc.getCenter().y).angle();

			SpeDynamicObject bullet = spe
					.addDynamicObject(new SpeDynamicObject());
			bullet.addPhysicsModule(new SpePhysicsDestroyOnTouch());
			bullet.physicsVector.set(new Vector2(0, 20).rotate(a - 90));
			bullet.setBounds(player.loc.getCenter().x,
					player.loc.getCenter().y, .25f, .25f);
			bullet.setPrimaryData("bullet");

			playerAnim.objectFlash(Color.RED, .5f);

			// ps.setGravity(0, 12);
			// ps.Explosion(temp.x, temp.y, .2f, 5);
			// ps.CreateEmitter("test", new Vector2(temp.x,temp.y), new
			// Vector2(0,5), 1, new Vector2(0,-5), Color.RED, Color.YELLOW, 3,
			// 2, 10f);
			// ps.createEmitter(new Vector2(temp.x,temp.y));
		}

		if (!paused) {
			cu.update(Gdx.graphics.getDeltaTime());

			ps.update(Gdx.graphics.getDeltaTime());

			spe.update(Gdx.graphics.getDeltaTime());

		}
		// Gdx.app.log("Number of contacts", spe.getContacts().size +"");

		spe.DrawCurrentMap();
		//spe.getCurrentMap().drawMapLayer(0);
		//spe.getCurrentMap().drawMapLayer(1);
		playerAnim.update();
		batch.setProjectionMatrix(camera.combined);
		batch.begin();
		//playerAnim.draw(batch);
		// playerAnim.draw(batch, 0, 0, 50, 50);
		batch.end();
	}

	@Override
	public void resize(int width, int height) {
	}

	@Override
	public void pause() {
		paused = true;
	}

	@Override
	public void resume() {
		paused = false;
	}
}
